
<template>
  <el-container class="card-container">
    <el-header class="header-style">
      <div class="header-content">
        <h2>银行卡管理</h2>
        <div class="button-group">
          <el-button type="primary" @click="openDialog">新增银行卡</el-button>
          <el-button type="success" @click="downloadExcel">下载Excel</el-button>
        </div>
      </div>
    </el-header>
    <el-main class="main-style">
      <el-table :data="cardData" border style="width: 100%">
        <el-table-column prop="cardID" label="卡号"></el-table-column>
        <el-table-column prop="curID" label="币种"></el-table-column>
        <el-table-column prop="username" label="用户名"></el-table-column>
        <el-table-column prop="openDate" label="开户日期"></el-table-column>
        <el-table-column prop="balance" label="余额"></el-table-column>
        <el-table-column prop="pass" label="密码"></el-table-column>
        <el-table-column prop="isReportLoss" label="挂失状态"></el-table-column>
        <el-table-column label="操作">
          <template #default="scope">
            <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
            <el-button size="small" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-main>
  </el-container>

  <!-- 新增/编辑银行卡对话框 -->
  <el-dialog v-model="dialogVisible" :title="editMode ? '编辑银行卡' : '新增银行卡'">
    <el-form :model="cardForm">
      <el-form-item label="卡号">
        <el-input v-model="cardForm.cardID" placeholder="请输入卡号"></el-input>
      </el-form-item>
      <el-form-item label="币种">
        <el-input v-model="cardForm.curID" placeholder="请输入币种"></el-input>
      </el-form-item>
      <el-form-item label="用户名">
        <el-input v-model="cardForm.username" placeholder="请输入用户名"></el-input>
      </el-form-item>
      <el-form-item label="开户日期">
        <el-date-picker v-model="cardForm.openDate" type="date" placeholder="选择日期"></el-date-picker>
      </el-form-item>
      <el-form-item label="余额">
        <el-input v-model="cardForm.balance" placeholder="请输入余额"></el-input>
      </el-form-item>
      <el-form-item label="密码">
        <el-input v-model="cardForm.pass" placeholder="请输入密码"></el-input>
      </el-form-item>
      <el-form-item label="挂失状态">
        <el-select v-model="cardForm.isReportLoss" placeholder="请选择状态">
          <el-option label="是" value="是"></el-option>
          <el-option label="否" value="否"></el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="submitCard">确认</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script>
import { ref, onMounted } from 'vue';
import axios from 'axios';
import * as XLSX from 'xlsx';

export default {
  name: 'CardView',
  setup() {
    const cardData = ref([]);
    const dialogVisible = ref(false);
    const editMode = ref(false);
    const currentRow = ref(null);
    const cardForm = ref({
      cardID: '',
      curID: '',
      username: '',
      openDate: '',
      balance: '',
      pass: '',
      isReportLoss: ''
    });

    // 从后端获取银行卡数据
    const fetchCards = async () => {
      try {
        const response = await axios.get('http://localhost:8088/api/cards');
        cardData.value = response.data;
      } catch (error) {
        console.error('获取银行卡数据失败:', error);
      }
    };

    // 生命周期钩子：组件挂载后获取银行卡数据
    onMounted(() => {
      fetchCards();
    });

    const handleEdit = (index, row) => {
      editMode.value = true;
      currentRow.value = row;
      cardForm.value = { ...row };
      dialogVisible.value = true;
    };

    const handleDelete = (index, row) => {
      axios.delete(`http://localhost:8088/api/cards/delete/${row.cardID}`)
          .then(response => {
            console.log('删除银行卡成功:', response.data);
            cardData.value = cardData.value.filter(item => item.cardID !== row.cardID);
          })
          .catch(error => {
            console.error('删除银行卡失败:', error);
          });
    };

    const openDialog = () => {
      editMode.value = false;
      currentRow.value = null;
      cardForm.value = { cardID: '', curID: '', username: '', openDate: '', balance: '', pass: '', isReportLoss: '' };
      dialogVisible.value = true;
    };

    const submitCard = () => {
      const cardInfo = {
        cardID: cardForm.value.cardID,
        curID: cardForm.value.curID,
        username: cardForm.value.username,
        openDate: cardForm.value.openDate,
        balance: cardForm.value.balance,
        pass: cardForm.value.pass,
        isReportLoss: cardForm.value.isReportLoss
      };

      if (editMode.value && currentRow.value) {
        axios.put(`http://localhost:8088/api/cards/update/${currentRow.value.cardID}`, cardInfo)
            .then(response => {
              console.log('更新银行卡成功:', response.data);
              const index = cardData.value.findIndex(item => item.cardID === currentRow.value.cardID);
              if (index !== -1) {
                cardData.value[index] = { ...cardForm.value, cardID: currentRow.value.cardID };
              }
            })
            .catch(error => {
              console.error('更新银行卡失败:', error);
            });
      } else {
        axios.post('http://localhost:8088/api/cards/insert', cardInfo)
            .then(response => {
              console.log('新增银行卡成功:', response.data);
              cardData.value.push({ ...cardForm.value });
            })
            .catch(error => {
              console.error('新增银行卡失败:', error);
            });
      }
      dialogVisible.value = false;
    };

    // 下载Excel
    const downloadExcel = () => {
      const workbook = XLSX.utils.book_new();
      const worksheet = XLSX.utils.json_to_sheet(cardData.value);
      XLSX.utils.book_append_sheet(workbook, worksheet, '银行卡信息');
      XLSX.writeFile(workbook, '银行卡信息.xlsx');
    };

    return {
      cardData,
      dialogVisible,
      editMode,
      currentRow,
      cardForm,
      handleEdit,
      handleDelete,
      openDialog,
      submitCard,
      downloadExcel
    };
  },
};
</script>

<style scoped>
.card-container {
  height: 100vh;
  display: flex;
  flex-direction: column;
}

.header-style {
  background-color: #fff;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  padding: 10px 20px;
  position: sticky;
  top: 0;
  z-index: 99;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.header-content {
  display: flex;
  flex: 1;
  justify-content: space-between;
  align-items: center;
}

.main-style {
  flex: 1;
  padding: 20px;
  overflow-y: auto;
}

.el-table {
  height: 100%;
}

.button-group {
  display: flex;
  gap: 10px;
}

.dialog-footer {
  display: flex;
  justify-content: flex-end;
}
</style>